﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;

using FRL.DebtChased.Silverlight.Library;
using FRL.DebtChased.Silverlight.Library.RichContentService;
using System.ComponentModel;


namespace FRL.DebtChased.ChaseWorkspace
{
    public partial class CommentsPanelCtrl : UserControl, INotifyPropertyChanged
    {
        /////////////////////////////////////////////////////////////
        // Interface & Constructor

        public event PropertyChangedEventHandler PropertyChanged;
        public void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        public CommentsPanelCtrl()
        {
            InitializeComponent();
            ObjectNoteService.ObjectNoteDisplayRequest += new ObjectNoteService.ObjectNoteDisplayRequestEventHandler(OnObjectNoteDisplayRequest);
            this.LayoutRoot.DataContext = this;

            DebtorNotes = new CollectionViewSource();
            DebtorNotes.SortDescriptions.Add(new SortDescription("Timestamp", ListSortDirection.Descending));
        }
        ~CommentsPanelCtrl()
        {
            ObjectNoteService.ObjectNoteDisplayRequest -= this.OnObjectNoteDisplayRequest;
        }

        /////////////////////////////////////////////////////////////
        // Control "Parameter"

        private AccountTransport _account;
        public AccountTransport Account
        {
            get { return _account; }
            set
            {
                if ((object.ReferenceEquals(this._account, value) != true))
                {
                    this._account = value;
                    RaisePropertyChanged("Account");

                    ObjectNotes.Clear();
                    DebtorNotes.Source = Account.Notes;
                    this.DebtorNotes.View.Refresh();
                    this.AccountNotesPanel.ItemsSource = DebtorNotes.View;
                }
            }
        }

        CollectionViewSource DebtorNotes;

        private ObservableCollection<ObservableCollection<AccountNoteTransport>> _objectNotes = new ObservableCollection<ObservableCollection<AccountNoteTransport>>();
        public ObservableCollection<ObservableCollection<AccountNoteTransport>> ObjectNotes
        {
            get { return _objectNotes; }
            set
            {
                if ((object.ReferenceEquals(this._account, value) != true))
                {
                    this._objectNotes = value;
                    RaisePropertyChanged("ObjectNotes");
                }
            }
        }

        /////////////////////////////////////////////////////////////
        // Event Handlers

        private void AddCommentButton_Click(object sender, RoutedEventArgs e)
        {
            AddNoteDlg dlg = new AddNoteDlg(Account);
            dlg.Show();
        }

        private void OnObjectNoteDisplayRequest(object sender, ObjectNoteDisplayRequestEventArgs e)
        {
            if (e.Notes != null)
            {
                if (ObjectNotes.Contains(e.Notes))
                    ObjectNotes.Remove(e.Notes);
                else
                    ObjectNotes.Add(e.Notes);
            }
        }

        private void CreditCardPayment_Click(object sender, RoutedEventArgs e)
        {

            CreditCardProcessingDlg dlg = new CreditCardProcessingDlg();
            dlg.Show();
        }    
    }
}
